<template>
  <div class="container">
    <div class="stats">
      <div class="stat">
        <div>已完成订单</div>
        <div class="count">
          <countTo :startVal="0" :endVal="213" :duration="2000" />
        </div>
      </div>
      <div class="stat">
        <div>计划完成订单</div>
        <div class="count">
          <countTo :startVal="0" :endVal="353" :duration="2000" />
        </div>
      </div>
    </div>
    <div ref="chart" class="chart"></div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onBeforeUnmount } from 'vue'
import * as echarts from 'echarts'
import { CountTo } from 'vue3-count-to'

// 响应式引用
const chart = ref<HTMLDivElement | null>(null)
let myChart: echarts.ECharts | null = null

// 初始化图表
const initChart = () => {
  if (chart.value) {
    myChart = echarts.init(chart.value)
    setChartOptions()
  }
}

// 设置图表选项
const setChartOptions = () => {
  const option = {
    tooltip: {
      trigger: 'axis'
    },
    legend: {
      data: ['已完成订单', '计划完成订单']
    },
    xAxis: {
      type: 'category',
      data: ['1月', '2月', '3月', '4月', '5月', '6月', '7月', '8月']
    },
    yAxis: {
      type: 'value'
    },
    grid: {
      left: '50px',
      right: '50px',
      top: '40px',
      bottom: '40px'
    },
    series: [
      {
        name: '已完成订单',
        type: 'line',
        data: [4, 2, 23, 6, 11, 12, 2, 32],
        smooth: true,
        itemStyle: {
          color: '#56E6A6'
        },
        lineStyle: {
          width: 5,
          shadowColor: 'rgba(0, 0, 0, 0.3)',
          shadowBlur: 10,
          shadowOffsetY: 10,
          color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
            { offset: 0, color: '#56E6A6' },
            { offset: 1, color: '#3798E3' }
          ])
        }
      },
      {
        name: '计划完成订单',
        type: 'line',
        data: [4, 23, 6, 22, 7, 2, 22, 22],
        smooth: true,
        itemStyle: {
          color: '#3798E3'
        },
        lineStyle: {
          width: 5,
          shadowColor: 'rgba(0, 0, 0, 0.3)',
          shadowBlur: 10,
          shadowOffsetY: 10,
          color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
            { offset: 0, color: '#3798E3' },
            { offset: 1, color: '#56E6A6' }
          ])
        }
      }
    ]
  }
  if (myChart) {
    myChart.setOption(option)
  }
}

// 处理窗口大小调整
const handleResize = () => {
  if (myChart) {
    myChart.resize()
  }
}

// 生命周期钩子
onMounted(() => {
  initChart()
  window.addEventListener('resize', handleResize)
})

onBeforeUnmount(() => {
  window.removeEventListener('resize', handleResize)
})
</script>

<style scoped>
.container {
  width: 100%;
  height: 20vh;
}

.stats {
  display: flex;
  justify-content: space-around;
  align-items: center;
}

.stat {
  text-align: center;
}

.count {
  font-size: 24px;
  font-weight: bold;
}

.chart {
  width: 100%;
  height: 100%;
}

/* Ensure the parent container takes up available space */
html,
body,
#app {
  height: 100%;
  margin: 0;
}
</style>
